<admintpl file="header" />
<style type="text/css">
.pic-list li {
	margin-bottom: 5px;
}
</style>
<script type="text/html" id="photos-item-wrapper">
	<li id="savedimage{id}">
		<input id="photo-{id}" type="hidden" name="photos_url[]" value="{filepath}"> 
		<input id="photo-{id}-name" type="text" name="photos_alt[]" value="{name}" style="width: 160px;" title="图片名称">
		<img id="photo-{id}-preview" src="{url}" style="height:36px;width: 36px;" onclick="parent.image_preview_dialog(this.src);">
		<a href="javascript:upload_one_image('图片上传','#photo-{id}');">替换</a>
		<a href="javascript:(function(){$('#savedimage{id}').remove();})();">移除</a>
	</li>
</script>
</head>
<body>
	<div class="wrap js-check-wrap">
		<ul class="nav nav-tabs">
			<li><a href="{:U('Questions/index')}">考题管理</a></li>
			<li><a href="{:U('Questions/add',array('term'=>empty($term['term_id'])?'':$term['term_id']))}" target="_self">添加考题</a></li>
			<li><a href="{:U('Questions/import')}" target="_self">批量导入</a></li>
			<li class="active"><a href="#">编辑考题</a></li>
		
		</ul>
		<form action="{:U('Questions/edit_question')}" method="post" class="form-horizontal js-ajax-forms">
			<div class="row-fluid">
				<div class="span9">
				<input type="hidden" name="ques_id" value="{$ques_id}"/>
					<table class="table table-bordered">
						<tr>
							<th width="80">试题分类</th>
							<td>
								<select style="max-height: 100px;" name="ques_term">{$taxonomys}</select>
							</td>
						</tr>
						<tr>
							<th width="80">试题类型</th>
							<td>
								<select style="max-height: 100px;" name="ques_type" id="question_type" onchange="showlist()">
								<option value="0">选择试题类型</option>
								<foreach name="question_types" item="vo">								
						        	<option value="{$key}" <eq name="ques.ques_type" value="$key">selected</eq>>{$vo}</option>
								</foreach>
								</select>
							</td>
						</tr>
						<tr id="question_option_count">
							<th width="80">选项数目</th>
							<td>
								<select style="max-height: 100px;" name="option_count" id="option_count" onchange="optionList(this);">
								<foreach name="option_count" item="vo">
								<option <eq name="ques.option_count" value="$vo">selected</eq>>{$vo}</option>
								</foreach>
								</select>
							</td>
						</tr>
						<tr>
							<th width="80">试题状态</th>
							<td>
								<select style="max-height: 100px;" name="status">
								<option value="0" <eq name="ques.ques_status" value="0">selected</eq>>启用</option>
								<option value="1" <eq name="ques.ques_status" value="1">selected</eq>>停用</option>
								</select>
							</td>
						</tr>			
						<tr>
							<th width="80">试题难度</th>
							<td>
								<select style="max-height: 100px;" name="ques_level" id="ques_level">
								<foreach name="question_level" item="vo">
								<option value="{$key}" <eq name="ques.ques_level" value="$key">selected</eq>>{$vo}</option>
								</foreach>
								</select>
							</td>
						</tr>
						<tr>
							<th>默认分数</th>
							<td>
								<input type="text" style="width:200px;" name="ques_score" id="title" required value="{$ques.ques_score}" placeholder="请输入分数"/>分
								<span class="form-required">*</span>
							</td>
						</tr>
						<tr>
							<th>排序</th>
							<td><input type="text" name="listorder" id="listorder" value="{$ques.listorder}" style="width: 200px" placeholder="选填">值越小越先前 </td>
						</tr>
						<tr>
							<th>题干</th>
							<td>
								<script type="text/plain" id="ques_content" name="ques_content">{$ques.ques_content}</script>
							</td>
						</tr>
						<tr id="question_options" style="display:none">
							<th>答案列表</th>
							<td>
							<div  class="lineD" style="display:none" id="single_option"><ol type="A"></ol></dd>
							</div>
							<div   class="lineD" style="display:none" id="many_option"><ol type="A"></ol>
							</div>
							
							请勾选正确答案。
							</td>
						</tr>
						<tr id="empty_options" style="display:none">
							<th>填空题答案</th>
							<td>
							<div   class="lineD" id="empty_option"><ol></ol>
							</div>							
							请选择答案数量后，在此填写正确答案。
							</td>
						</tr>
						<tr>
							<th>答案解析</th>
							<td>
								<textarea name="ques_guide" id="description" style="width: 98%; height: 50px;" placeholder="请填写摘要">{$ques.ques_guide}</textarea>
							</td>
						</tr>
						<tr>
							<th>考题来源</th>
							<td><input type="text" name="ques_source" id="source" value="{$ques.ques_source}" style="width: 400px" placeholder="请输入文章来源"></td>
						</tr>
					</table>
				</div>
			</div>
			<div class="form-actions">
				<button class="btn btn-primary js-ajax-submit" type="submit">提交</button>
				<a class="btn" href="{:U('Questions/index')}">返回</a>
			</div>
		</form>
	</div>
	<script type="text/javascript" src="__PUBLIC__/js/common.js"></script>
	<script type="text/javascript">
		//编辑器路径定义
		var editorURL = GV.WEB_ROOT;
	</script>
	<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.frontend.config.js"></script>
	<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.all.min.js"></script>
	<script type="text/javascript">
	var single=document.getElementById('single_option');
	var many=document.getElementById('many_option');
	var empty=document.getElementById('empty_option');
	var olBox=single.getElementsByTagName('ol')[0];
	var emBox=many.getElementsByTagName('ol')[0];
	var eyBox=empty.getElementsByTagName('ol')[0];
	var selNum=document.getElementById('option_count').value;
	var qid = 0;
	if( qid == 0 ) {
		for(var i=0;i<selNum;i++){
			olBox.innerHTML+='<li><input type="radio" name="opt" value="'+(i+1)+'"><input style="width:300px;" type="text" name="single_question_answer[]" class="text0'+(i+1)+'"></li>';
			emBox.innerHTML+='<li><input type="checkbox" name="opts[]" value="'+(i+1)+'"><input style="width:300px;" type="text" name="many_question_answer[]" class="text0'+(i+1)+'"></li>';
			eyBox.innerHTML+='<li><input style="width:300px;" type="text" name="empty_question_answer[]" class="text0'+(i+1)+'"></li>';
		}
	}
	function optionList(element){
		var selNum=element.value;
		olBox.innerHTML='';	
		emBox.innerHTML='';
		eyBox.innerHTML='';
		for(var i=0;i<selNum;i++){
			olBox.innerHTML+='<li><input type="radio" name="opt" value="'+(i+1)+'"><input style="width:300px;" type="text" name="single_question_answer[]" class="text0'+(i+1)+'"></li>';
			emBox.innerHTML+='<li><input type="checkbox" name="opts[]" value="'+(i+1)+'"><input style="width:300px;" type="text" name="many_question_answer[]" class="text0'+(i+1)+'"></li>';
			eyBox.innerHTML+='<li><input style="width:300px;" type="text" name="empty_question_answer[]" class="text0'+(i+1)+'"></li>';
		}
	}
	var ans=document.getElementById('box_answer');
	function removeLi(elem){
		var elementLi=elem.parentNode;				//获取当前a元素的父节点
		ans.removeChild(elementLi);				    //删除这个li节点
		var val = $(elem).data('num');
		var str = E.getData();
		if( str.indexOf(val) != -1 ) {
			E.setData(str.replace('<img class="tkimg" alt="'+val+'" />' , ''));
		}
	}

	function showlist(){

		var options={$ques_options};
		var option_count=$('#option_count').val();
		var answer='{$ques.answer}';
        var o = document.getElementById('question_type');
        var strValue = o.options[o.options.selectedIndex].value;
        if(strValue == "1" || strValue == "4") {
            document.getElementById('question_option_count').style.display = "";
            document.getElementById('single_option').style.display = "";

            document.getElementById('question_options').style.display = "";
            document.getElementById('many_option').style.display = "none";
            if( strValue == "4" ) {
            	document.getElementById('option_count').disabled = true;
            	document.getElementById('option_count').value=2;
                document.getElementById('empty_options').style.display = "none";
            	document.getElementById('option_count').onchange();
            }
            for(var i=0;i<option_count;i++){
            	$("#single_option input.text0"+(i+1)).val(options[i]);
            }
        }
        else if(strValue == "2") {
            document.getElementById('question_option_count').style.display = "";
            document.getElementById('question_options').style.display = "";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "";
            document.getElementById('empty_options').style.display = "none";
            document.getElementById('option_count').disabled = false;

            for(var i=0;i<option_count;i++){
            	$("#many_option input.text0"+(i+1)).val(options[i]);
            }

            var answer="{$ques.answer}";
            for(var i=0;i<option_count;i++){
            	if(answer.indexOf(i+'')>-1){
            		$("#many_option :checkbox[value='"+i+"']").attr("checked",true);
            	}
            }
        }
        else if(strValue == "3") {
            document.getElementById('question_option_count').style.display = "";
            document.getElementById('question_options').style.display = "";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('empty_options').style.display = "";
            document.getElementById('option_count').disabled = false;

            for(var i=0;i<option_count;i++){
            	$("#empty_option input.text0"+(i+1)).val(options[i]);
            }
        }
		else if (strValue == "0") {
			document.getElementById('question_option_count').style.display = "none";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('question_options').style.display = "none";
            document.getElementById('empty_options').style.display = "none";
		}
		else {
            document.getElementById('question_option_count').style.display = "none";
            document.getElementById('single_option').style.display = "none";
            document.getElementById('many_option').style.display = "none";
            document.getElementById('question_options').style.display = "none";
            document.getElementById('empty_options').style.display = "none";
        }
    }


	$(function() {
		showlist();		
					
		$(".js-ajax-close-btn").on('click', function(e) {
			e.preventDefault();
			Wind.use("artDialog", function() {
				art.dialog({
					id : "question",
					icon : "question",
					fixed : true,
					lock : true,
					background : "#CCCCCC",
					opacity : 0,
					content : "您确定需要关闭当前页面嘛？",
					ok : function() {
						setCookie("refersh_time", 1);
						window.close();
						return true;
					}
				});
			});
		});
		/////---------------------
		Wind.use('validate', 'ajaxForm', 'artDialog', function() {
			//javascript
			//编辑器
			var editor = UE.getEditor('ques_content');
			//增加编辑器验证规则
			jQuery.validator.addMethod('editorcontent', function() {
				return editorcontent.hasContents();
			});
			var form = $('form.js-ajax-forms');
			//ie处理placeholder提交问题
			if ($.browser && $.browser.msie) {
				form.find('[placeholder]').each(function() {
					var input = $(this);
					if (input.val() == input.attr('placeholder')) {
						input.val('');
					}
				});
			}
			var formloading = false;
			//表单验证开始
			form.validate({
				//是否在获取焦点时验证
				onfocusout : false,
				//是否在敲击键盘时验证
				onkeyup : false,
				//当鼠标掉级时验证
				onclick : false,
				//验证错误
				showErrors : function(errorMap, errorArr) {
					//errorMap {'name':'错误信息'}
					//errorArr [{'message':'错误信息',element:({})}]
					try {
						$(errorArr[0].element).focus();
						art.dialog({
							id : 'error',
							icon : 'error',
							lock : true,
							fixed : true,
							background : "#CCCCCC",
							opacity : 0,
							content : errorArr[0].message,
							cancelVal : '确定',
							cancel : function() {
								$(errorArr[0].element).focus();
							}
						});
					} catch (err) {
					}
				},
				//验证规则
				rules : {
					ques_term : {
						required : true
					},
					ques_type : {
						required : true
					},
					ques_score : {
						required : true
					},
					ques_content : {
						required : true,
						editorcontent : true
					},
	// 				opt : {
	// 					required : $('#ques_type').val()==1||$('#ques_type').val()==4
	// 				},
	// 				'opts[]' : {
	// 					required : $('#ques_type').val()==2
	// 				},
					
				},
				//验证未通过提示消息
				messages : {
					ques_term : {
						required : '请选择考题分类'
					},
					ques_type : {
						required : '请选择题型'
					},
					ques_score : {
						required : '请填写试题分数'
					},
					ques_content : {
						required : '请填写题干',
						editorcontent : '内容不能为空'
					}			
					
				},
	// 			errorPlacement: function (error, element) { //指定错误信息位置
	// 				if (element.is(':radio') || element.is(':checkbox')) {
	// 					var eid = element.attr('name');
	// 					error.appendTo(element.parent());
	// 				} else {
	// 					error.insertAfter(element);
	// 				}
	// 			},
				//给未通过验证的元素加效果,闪烁等
				highlight : false,
				//是否在获取焦点时验证
				onfocusout : false,
				//验证通过，提交表单
				submitHandler : function(forms) {
					if (formloading)
						return;
					$(forms).ajaxSubmit({
						url : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
						dataType : 'json',
						beforeSubmit : function(arr, $form, options) {
							formloading = true;
						},
						success : function(data, statusText, xhr, $form) {
							formloading = false;
							if (data.status) {
								setCookie("refersh_time", 1);
								//添加成功
								Wind.use("artDialog", function() {
									art.dialog({
										id : "succeed",
										icon : "succeed",
										fixed : true,
										lock : true,
										background : "#CCCCCC",
										opacity : 0,
										content : data.info,
										button : [ {
											name : '返回列表页',
											callback : function() {
												location = "{:U('Questions/index')}";
												return true;
											}
										} ]
									});
								});
							} else {
								artdialog_alert(data.info);
							}
						}
					});
				}
			});
			$("input[name='opt'][value='{$ques.answer}']").attr('checked','true');
			$("input[name='opts[]']").each(function(index,element){
				var strs= new Array(); //定义一数组 
				strs='{$ques.answer}'.split(","); //字符分割 
				for (var i=0;i<strs.length;i++ ) 
				{					
					if($(this).val()==strs[i]){
						$(this).attr('checked','true');
					}
				} 

			});
			$('#ques_type').change(function(){
				if($(this).val()==4){
					$('#single_option').find(":text").each(function(index,element){
						if(index==0)
							$(this).val('正确');
						else
							$(this).val('不正确');
					});
				}
				else{
					$('#single_option').find(":text").each(function(index,element){
						$(this).val('');
					});
				}
			});
		});
		////-------------------------
	});
	</script>
</body>
</html>